#include "menu.h"
#include "Chargement.h"
#include "collision.h"
int main()
{
    int continuer=0,back_indice=0;
    int position_right=0;
    int position_left=0;
    int direction=0; //0 right 1 left
    int x=10,y1=200;
    int xcolld,ycolld,xcollde,ycollde;
    int enemy_time1;
    int enemy_pos1,enemy_pos2=350;
    int pos_bullet=80,bullet_state;
    BITMAP *enemy;
    int i=0;
    BITMAP *cartouche;
    int score=0 ;
    int money[20];
    initialiser();
    SAMPLE *son, *gamplay1;
    install_sound(DIGI_AUTODETECT, MIDI_NONE,NULL);
    son=load_sample("son.wav");
    play_sample(son,255, 150, 1000,1);
    BITMAP* chuck[6] ,*Background[3], *coll[3];
    int choix=0;

    Menu monMenu;
    chargerMenu(&monMenu);
    chargement(Background,coll,chuck);
    int quiter=0;
    while(!quiter)
    {
        menu(&monMenu, &choix);
        if(choix==0)
        {
            quiter=1;
        }
        if(choix==1)
        {
            BITMAP *help=load_bitmap("menu_help.bmp", NULL);
            blit(help,screen,0,0,0,0,SCREEN_W,SCREEN_H);
            while(!key[KEY_ESC]) { }
        }
        if(choix==2)
        {
            exit(0);
        }
    }
    stop_sample(son);
    cartouche=load_bitmap("bullet.bmp",NULL);

    enemy=load_bitmap("fantome.bmp",NULL);
    enemy_pos1=SCREEN_W;
    enemy_time1=Random(20,30);
    BITMAP* buffer;

    buffer=create_bitmap(8400,600);

    blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
    blit(Background[back_indice],buffer,0,0,0,0,SCREEN_W,SCREEN_H);
    draw_sprite(buffer,chuck[0],SCREEN_W/2,y1);
    gamplay1=load_sample("gameplay1.wav");
    play_sample(gamplay1,255, 150, 1000,1);
    while (continuer==0)
    {
        if (key[KEY_RIGHT] )
        {
            xcolld=x+chuck[0]->w;
            ycolld=y1+chuck[0]->h/2;

            if ((get_pixel(coll[back_indice],xcolld,ycolld)!=255))
            {
                direction=0;
                if(position_right<2)
                {
                    position_right++;
                }
                else position_right=0;
                if(x<4150)
                    x=x+7;
            }
            if (x>4150 && back_indice<3)
            {
                x=20;
                if (back_indice!=3)
                    back_indice++;
            }
        }
        if (x<10 && back_indice>0)
        {
            x=SCREEN_W-95;
            back_indice--;
        }
        if (key[KEY_LEFT])
        {
            direction=1;
            if(position_left<5)position_left++;
            else position_left=3;
            if (x>0)
                x--;
        }
        if (key[KEY_SPACE])
        {
            bullet_state=1;
        }
        textprintf_ex(screen, font, 10, 10, makecol(255, 255, 255),-1, "Your score: %d", score);
        if ( pos_bullet ==enemy_pos2)
        {
            score=score+50;
        }

        if (key[KEY_ESC])
        {
            MenuPause(&continuer,back_indice,x,direction,score);
        }


        xcollde=x+ chuck[0]->w /2;
        ycollde=y1 + chuck[0]->h;
        if (!key[KEY_UP] && get_pixel(coll[back_indice],xcollde,ycollde)!=255)
            y1++;
        else
            y1--;
        /*if ((key[KEY_UP])&&
         get_pixel(coll[back_indice],xcoll,ycoll)==255 )
        y1--;
        /*else if (y1!=300)
        y1++;
        */




        blit(Background[back_indice],buffer,x,0,0,0,8400,SCREEN_H);
        if (direction==0)
        {
            draw_sprite(buffer,chuck[position_right],x,y1);
        }
        else if(direction==1)
        {
            draw_sprite(buffer,chuck[position_left],x,y1);
        }
        if ((pos_bullet>enemy_pos1) || (y1+18==enemy_pos2))
            enemy_pos1=enemy_pos1+500 ;
        if(i>enemy_time1)
        {
            enemy_pos1--;
            draw_sprite(buffer,enemy,enemy_pos1,enemy_pos2 );
        }
        if (enemy_pos1<x)
        {

            gameover(buffer,&continuer);
        }
        if (pos_bullet>(x+300))
        {
            bullet_state=0;
            pos_bullet=x+80;
        }
        if (bullet_state==1)
        {
            pos_bullet+=10;
            draw_sprite(buffer,cartouche,pos_bullet,y1+110);
        }
        blit(buffer,screen,x,0,0,0,SCREEN_W,SCREEN_H);
        clear_bitmap(buffer);
        i++;
    }
    stop_sample(son);
    return 0;
}
END_OF_MAIN ();
